package com.jdj.framework.core.base.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanMap;

/**
 * Map 工具类
 * @author caijinbang
 * @date 2019-04-13 15:40
 */
public class MapUtils extends org.apache.commons.collections.MapUtils {

    private static Logger logger = LoggerFactory.getLogger(MapUtils.class);



    /**
     * 提取集合中的对象的属性(通过Getter函数), 组合成Map.
     *
     * @param collection        来源集合.
     * @param keyPropertyName   要提取为Map中的Key值的属性名.
     * @param valuePropertyName 要提取为Map中的Value值的属性名.
     */
    public static Map extractToMap(Collection collection, String keyPropertyName, String valuePropertyName) {
        Map map = new HashMap();
        try {
            for (Object obj : collection) {
                map.put(PropertyUtils.getProperty(obj, keyPropertyName), PropertyUtils.getProperty(obj, valuePropertyName));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return map;
    }


    /**
     * 将对象装换为map
     *
     * @param bean
     * @return
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = new HashMap<>();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key.toString(), beanMap.get(key));
            }
        }
        return map;
    }

    /**
     * 将map装换为bean对象
     *
     * @param map  映射
     * @param bean 对象
     * @param <T>
     * @return
     */
    public static <T> T mapToBean(Map<String, Object> map, T bean) {
        if (map == null) {
            return null;
        }
        BeanMap beanMap = BeanMap.create(bean);
        beanMap.putAll(map);
        return bean;
    }

    /**
     * 将map装换为bean对象
     *
     * @param map       映射
     * @param beanClass 对象类
     * @param <T>
     * @return
     */
    public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) {
        try {
            T bean = beanClass.newInstance();
            return mapToBean(map, bean);
        } catch (InstantiationException | IllegalAccessException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 将List<T>转换为List<Map<String, Object>>
     *
     * @param objList 对象集合
     * @return
     */
    public static <T> List<Map<String, Object>> beanListToMapList(List<T> objList) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (objList != null && objList.size() > 0) {
            Map<String, Object> map;
            T bean;
            for (T object : objList) {
                bean = object;
                map = beanToMap(bean);
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 将List<Map<String,Object>>转换为List<T>
     *
     * @param maps  Map对象集合
     * @param clazz Bean 的 Class
     * @return
     */
    public static <T> List<T> mapListToBeanList(List<Map<String, Object>> maps, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        if (maps != null && maps.size() > 0) {
            T bean;
            try {
                for (Map<String, Object> map1 : maps) {
                    bean = clazz.newInstance();
                    list.add(mapToBean(map1, bean));
                }
            } catch (InstantiationException | IllegalAccessException e) {
                logger.error(e.getMessage(), e);
            }
        }
        return list;
    }
}
